/*
 * bsp_canfd.c
 *
 *  Created on: 2025 Jul 5
 *      Author: SFLYs
 */
#include "bsp_canfd.h"


void Bsp_CANFD_Pin_Init(void)
{
      GPIO_MuxConfig(4, GPIO_4_CANA_TX, GPIO_PIN_TYPE_STD, GPIO_QUAL_ASYNC);
      GPIO_setDirectionMode(4, GPIO_DIR_MODE_OUT);
      GPIO_MuxConfig(5, GPIO_5_CANA_RX, GPIO_PIN_TYPE_STD, GPIO_QUAL_ASYNC);
      GPIO_setDirectionMode(5, GPIO_DIR_MODE_IN);

      GPIO_MuxConfig(16, GPIO_16_CANB_TX, GPIO_PIN_TYPE_STD, GPIO_QUAL_ASYNC);
      GPIO_setDirectionMode(16, GPIO_DIR_MODE_OUT);
      GPIO_MuxConfig(17, GPIO_17_CANB_RX, GPIO_PIN_TYPE_STD, GPIO_QUAL_ASYNC);
      GPIO_setDirectionMode(17, GPIO_DIR_MODE_IN);
}


void Bsp_FLeg_CANFD_Init(void)
{
       CAN_Init_Config canInit;
       CANFD_Config canFd;
       // enable module clock
       SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_CANA);
       //Initializes CAN parameters.
       CAN_structInit(&canInit);
       CANFD_structInit(&canFd);
       canInit.canFDControl = CAN_FD_ENABLE;
       canFd.TDC = CAN_FD_TDC_ENABLE;
       canFd.SSPOffset = 16;
       canInit.ptrCanFd = &canFd;
       // Software reset.Set the bit rate and filter need the can in software reset mode.
       CAN_enableStatReset(CANFDA_BASE);
       //Initialize CAN module
       CAN_initModule(CANFDA_BASE, &canInit);
      // CAN_setAcceptFilter(CANFDB_BASE, CAN_ACF1, CAN_ID_STD, MOTOR_CAN_ID, MOTOR_CAN_ID_MASK);
       // (bundrate = pclk/s_prescale+1/(s_seg1+2 + s_seg2+1))
       //baund = 100000000/4/(38+2+9+1)
       //s_prescale:7
       //s_seg1: 38
       //s_seg2:9
       //s_sjw:9
       CAN_setBitTimingSlow(CANFDA_BASE,3, 38, 9, 9);  //1M 仲裁
       // (bundrate = pclk/f_prescale/(f_seg1+2 + f_seg2+1))
       //baund = 100000000/2/(18+2+4+1)
       //f_prescale:4
       //f_seg1: 18
       //f_seg2:4
       //f_sjw:4
       CANFD_setBitTimingFast(CANFDA_BASE, 1, 18, 4, 4);// 4M  数据
       // CAN enters normal communication mode.
       CAN_disableStatReset(CANFDA_BASE);
}

void Bsp_RLeg_CANFD_Init(void)
{
       CAN_Init_Config canInit;
       CANFD_Config canFd;
       // enable module clock
       SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_CANB);
       //Initializes CAN parameters.
       CAN_structInit(&canInit);
       CANFD_structInit(&canFd);
       canInit.canFDControl = CAN_FD_ENABLE;
       canFd.TDC = CAN_FD_TDC_ENABLE;
       canFd.SSPOffset = 16;
       canInit.ptrCanFd = &canFd;
       // Software reset.Set the bit rate and filter need the can in software reset mode.
       CAN_enableStatReset(CANFDB_BASE);
       //Initialize CAN module
       CAN_initModule(CANFDB_BASE, &canInit);
      // CAN_setAcceptFilter(CANFDB_BASE, CAN_ACF1, CAN_ID_STD, MOTOR_CAN_ID, MOTOR_CAN_ID_MASK);
       // (bundrate = pclk/s_prescale+1/(s_seg1+2 + s_seg2+1))
       //baund = 100000000/4/(38+2+9+1)
       //s_prescale:7
       //s_seg1: 38
       //s_seg2:9
       //s_sjw:9
       CAN_setBitTimingSlow(CANFDB_BASE, 3, 38, 9, 9);  //1M 仲裁
       // (bundrate = pclk/f_prescale/(f_seg1+2 + f_seg2+1))
       //baund = 100000000/2/(18+2+4+1)
       //f_prescale:4
       //f_seg1: 18
       //f_seg2:4
       //f_sjw:4
       CANFD_setBitTimingFast(CANFDB_BASE, 1, 18, 4, 4);// 4M  数据
       // CAN enters normal communication mode.
       CAN_disableStatReset(CANFDB_BASE);
}

void Bsp_CANFD_Init(void)
{
    Bsp_CANFD_Pin_Init();
    Bsp_FLeg_CANFD_Init();
    Bsp_RLeg_CANFD_Init();
}


void Bsp_FLeg_CANFD_SendMessage(uint32_t motor_id,uint8_t *data)
{
        CAN_TBUF_Ctrl tx1Ctrl;
        tx1Ctrl.TTSEN = CAN_TTSEN_DISABLE;
           tx1Ctrl.BRS = CANFD_BRS_FAST;
           tx1Ctrl.DLC = CAN_DLC8;
           tx1Ctrl.FDF = CAN_FDF_CANFD;
           tx1Ctrl.IDE = CAN_IDE_STANDARD;
           tx1Ctrl.RTR = CAN_RTR_DATA;
        CAN_fillMessage(CANFDA_BASE, CAN_TX_BUF_PTB,motor_id, &tx1Ctrl, data);
        CAN_startTx(CANFDA_BASE, CAN_TX_REQ_PTB);
        DEVICE_DELAY_US(1);
        //DEVICE_DELAY_US(250000);
}


uint32_t Bsp_FLeg_CANFD_RecvMessage(uint8_t *data)
{
    CAN_RBUF_Ctrl rxCtrl;
    uint32_t motor_id = 0;
    CAN_readMessageWithID(CANFDA_BASE,&motor_id, &rxCtrl,data);
    return motor_id;
}


void Bsp_RLeg_CANFD_SendMessage(uint32_t motor_id,uint8_t *data)
{
        CAN_TBUF_Ctrl tx1Ctrl;
        tx1Ctrl.TTSEN = CAN_TTSEN_DISABLE;
           tx1Ctrl.BRS = CANFD_BRS_FAST;
           tx1Ctrl.DLC = CAN_DLC8;
           tx1Ctrl.FDF = CAN_FDF_CANFD;
           tx1Ctrl.IDE = CAN_IDE_STANDARD;
           tx1Ctrl.RTR = CAN_RTR_DATA;
        CAN_fillMessage(CANFDB_BASE, CAN_TX_BUF_PTB,motor_id, &tx1Ctrl, data);
        CAN_startTx(CANFDB_BASE, CAN_TX_REQ_PTB);
        DEVICE_DELAY_US(1);
}

uint32_t Bsp_RLeg_CANFD_RecvMessage(uint8_t *data)
{
    CAN_RBUF_Ctrl rxCtrl;
    uint32_t motor_id = 0;
    CAN_readMessageWithID(CANFDB_BASE,&motor_id, &rxCtrl,data);
    return motor_id;
}
